<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        :root {
            /* 拖拽验证盒子的宽度 */
            --wd: 600px;
            /* 小盒子的宽高   */
            --sw: -webkit-calc(var(--wd) / 12)
        }

        .judgeContainer {
            width: var(--wd);
            margin: 100px auto;
        }

        .judgeContainer .judgeWraper {
            position: relative;
        }

        .judgeContainer .judgeWraper img {
            width: 100%;
        }

        .judgeContainer .judgeWraper .boxA,
        .judgeContainer .judgeWraper .boxB {
            width: var(--sw);
            height: var(--sw);
            position: absolute;
            top: 100px;
            background-color: #fff;
        }

        .boxA {
            left: 0;
            background: url(../images/bg_3.png) no-repeat -300px -100px;
            background-size: var(--wd) auto;
            z-index: 1;
        }

        .boxB {
            left: 300px;
        }


        .judgeContainer .judgeSlide {
            width: 100%;
            height: var(--sw);
            background-color: #CCCCCC;
            margin-top: 10px;
            border-radius: var(--sw);
            position: relative;
        }

        .ballC {
            width: var(--sw);
            height: var(--sw);
            border-radius: 50%;
            background-color: pink;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="judgeContainer">
        <div class="judgeWraper">
            <img class="bg" src="../images/bg_3.png" alt="">
            <div class="boxA"></div>
            <div class="boxB"></div>
        </div>
        <div class="judgeSlide">
            <div class="ballC"></div>
        </div>
    </div>
</body>
<script>
    // 等页面所有内容(文档 css js image font)加载完毕之后 执行事件
    var bg = document.getElementsByClassName("bg")[0];
    window.onload = function () {
        console.log(1111111111);
        var judgeContainer = document.querySelector(".judgeContainer");
        var judgeWraper = document.querySelector(".judgeWraper");
        var judgeSlide = document.querySelector(".judgeSlide");
        var boxA = document.querySelector(".boxA");
        var boxB = document.querySelector(".boxB");
        var ballC = document.querySelector(".ballC");

        // 问题: maxTop计算有误的问题 
        // 原因: judgeWraper 靠图片撑开 -> 但是如果js执行时,图片还没有加载完毕 , 图片并没有完全撑开judgeWraper => 导致judgeWraper高度获取有误
        // 解决方法
        // 1. 给图片设置定高 => 不符合当前页面自适应效果
        // 2. 等图片加载完毕之后在执行 => img.onload / window.onload

        var maxLeft = judgeSlide.clientWidth - ballC.clientWidth;
        var maxTop = judgeWraper.clientHeight - boxB.clientHeight;

        console.log(judgeWraper.clientHeight, boxB.clientHeight);

        // 页面加载时随机  
        // 100 - 550  => 随机范围 100(起始值) + [0,450](随机范围)
        // var randX = 100 + Math.round(Math.random()*(maxLeft-100))

        var randX = Math.round(maxLeft / 3 + Math.random() * (maxLeft - maxLeft / 3));
        var randY = Math.round(Math.random() * maxTop);

        boxB.style.left = randX + "px";
        boxB.style.top = randY + "px";
        boxA.style.top = randY + "px";
        boxA.style.backgroundPosition = `-${randX}px -${randY}px`;


        ballC.onmousedown = function (e) {
            var e = e || window.event;
            var posX = e.offsetX;
            console.log(posX);

            document.onmousemove = function (e) {
                var e = e || window.event;
                var x = e.pageX - judgeSlide.offsetLeft - posX;

                x = Math.min(Math.max(0, x), maxLeft);

                boxA.style.left = x + "px";
                ballC.style.left = x + "px";

            }
            document.onmouseup = function () {
                document.onmousemove = null;

                // 鼠标松开时  如果 boxA 和 boxB重合 => 验证成功 否则验证失败

                // boxA 和 boxB重合  => left值相等
                // 理想情况下 boxA.offsetLeft === boxB.offsetLeft  => 对手残党不友好
                // 可以设置误差允许范围: 
                // boxA.offsetLeft >= boxB.offsetLeft - 2 &&  boxA.offsetLeft <= boxB.offsetLeft + 2
                // Math.abs(boxA.offsetLeft - boxB.offsetLeft) < 2

                if (Math.abs(boxA.offsetLeft - boxB.offsetLeft) < 2) {
                    console.log("解锁成功");
                    location.href = "https://www.baidu.com";
                } else {
                    boxA.style.left = 0 + "px";
                    ballC.style.left = 0 + "px";

                    var randX = Math.round(maxLeft / 3 + Math.random() * (maxLeft - maxLeft / 3));
                    var randY = Math.round(Math.random() * maxTop);

                    boxB.style.left = randX + "px";
                    boxB.style.top = randY + "px";
                    boxA.style.top = randY + "px";
                    boxA.style.backgroundPosition = `-${randX}px -${randY}px`;
                }



            }

        }


    }

</script>

</html>